package com.xintoucloud.pbs.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xintoucloud.pbs.dao.RoleDao;
import com.xintoucloud.pbs.entity.Role;
@Repository
public class RoleDaoImpl implements RoleDao{
	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Role> list() {
		return sessionFactory.getCurrentSession().createQuery("from Role").list();
	}

	@Override
	public Serializable save(Role role) {
		return sessionFactory.getCurrentSession().save(role);
	}

	@Override
	public void update(Role role) {
		int rid = role.getRid();
		String name = role.getName();
		int parentId = role.getParentId_rid();
		sessionFactory.getCurrentSession().createQuery("update Role r set r.name='"+name+"', r.parentId_rid="+parentId+" where r.rid="+rid+" ").executeUpdate();
	}


	@Override
	public Role getOne(int id) {
		return sessionFactory.getCurrentSession().get(Role.class, id);
	}
	@Override
	public void delete(int id) {
		sessionFactory.getCurrentSession().delete(getOne(id));
		
	}

}
